<template>
  <el-dialog
    :title="!dataForm.countrysideId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="县区名称" prop="countrysideName">
      <el-input v-model="dataForm.countrysideName" placeholder="县区名称"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="xPosition">
      <el-input v-model="dataForm.xPosition" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="yPosition">
      <el-input v-model="dataForm.yPosition" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="第几次评选达标" prop="rsTime">
      <el-input v-model="dataForm.rsTime" placeholder="第几次评选达标"></el-input>
    </el-form-item>
    <el-form-item label="所属省份id" prop="provinceId">
      <el-input v-model="dataForm.provinceId" placeholder="所属省份id"></el-input>
    </el-form-item>
    <el-form-item label="所属市区id" prop="cityId">
      <el-input v-model="dataForm.cityId" placeholder="所属市区id"></el-input>
    </el-form-item>
    <el-form-item label="0表示被取消了资格，0表示未被取消资格" prop="isCancel">
      <el-input v-model="dataForm.isCancel" placeholder="0表示被取消了资格，0表示未被取消资格"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          countrysideId: 0,
          countrysideName: '',
          xPosition: '',
          yPosition: '',
          rsTime: '',
          provinceId: '',
          cityId: '',
          isCancel: ''
        },
        dataRule: {
          countrysideName: [
            { required: true, message: '县区名称不能为空', trigger: 'blur' }
          ],
          xPosition: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          yPosition: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          rsTime: [
            { required: true, message: '第几次评选达标不能为空', trigger: 'blur' }
          ],
          provinceId: [
            { required: true, message: '所属省份id不能为空', trigger: 'blur' }
          ],
          cityId: [
            { required: true, message: '所属市区id不能为空', trigger: 'blur' }
          ],
          isCancel: [
            { required: true, message: '0表示被取消了资格，0表示未被取消资格不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.countrysideId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.countrysideId) {
            this.$http({
              url: this.$http.adornUrl(`/info/countryside/info/${this.dataForm.countrysideId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.countrysideName = data.countryside.countrysideName
                this.dataForm.xPosition = data.countryside.xPosition
                this.dataForm.yPosition = data.countryside.yPosition
                this.dataForm.rsTime = data.countryside.rsTime
                this.dataForm.provinceId = data.countryside.provinceId
                this.dataForm.cityId = data.countryside.cityId
                this.dataForm.isCancel = data.countryside.isCancel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/info/countryside/${!this.dataForm.countrysideId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'countrysideId': this.dataForm.countrysideId || undefined,
                'countrysideName': this.dataForm.countrysideName,
                'xPosition': this.dataForm.xPosition,
                'yPosition': this.dataForm.yPosition,
                'rsTime': this.dataForm.rsTime,
                'provinceId': this.dataForm.provinceId,
                'cityId': this.dataForm.cityId,
                'isCancel': this.dataForm.isCancel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
